from pyswmm import Simulation, Output
from swmm.toolkit.shared_enum import NodeAttribute
import datetime
import sys
def get_updated_series(series):
    updated_series = {}
    for timestamp, value in series.items():
        if timestamp.second == 59:
            updated_timestamp = timestamp + datetime.timedelta(seconds=1)
            updated_series[updated_timestamp] = value
        else:
            updated_series[timestamp] = value
    return updated_series

def get_node_data(out, key):
    hydraulic_head_series = out.node_series(out.nodes[key], NodeAttribute.HYDRAULIC_HEAD)
    total_inflow_series = out.node_series(out.nodes[key], NodeAttribute.TOTAL_INFLOW)
    invert_depth_series = out.node_series(out.nodes[key], NodeAttribute.INVERT_DEPTH)

    converted_dict2 = {"shuiwei": get_updated_series(hydraulic_head_series)}
    converted_dict4 = {"liuliang": get_updated_series(total_inflow_series)}
    converted_dict6 = {"shuitou": get_updated_series(invert_depth_series)}
    # 跳过每个系列的前71个元素
    skip_count = 71
    my_dict2 = {key.strftime('%Y-%m-%d %H:%M'): value for i, (key, value) in enumerate(converted_dict2["shuiwei"].items()) if i >= skip_count}
    my_dict4 = {key.strftime('%Y-%m-%d %H:%M'): value for i, (key, value) in enumerate(converted_dict4["liuliang"].items()) if i >= skip_count}
    my_dict6 = {key.strftime('%Y-%m-%d %H:%M'): value for i, (key, value) in enumerate(converted_dict6["shuitou"].items()) if i >= skip_count}
    return key, {"shuiwei": my_dict2, "liuliang": my_dict4, "shuitou": my_dict6}


MODEL_INP_FILE = sys.argv[1]
#MODEL_INP_FILE = "43060030012-A4.inp"
model = Simulation(MODEL_INP_FILE)
model.execute()
out = Output(MODEL_INP_FILE.replace('inp', 'out'))
out.open()
dicts = {}

for key in out.nodes.keys():
    node_data = get_node_data(out, key)
    dicts[node_data[0]] = node_data[1]

print(dicts)